
function frac =dataAnalysis (subject)


%subject='laura_test_Adaptive_50ms_300trials'; %laura_tpp25ms_22_29_300';

qtyTrials=350;%300;%20;
sequenceSize=20;%40;
maleStart=18; %22;%21;
femaleStart=28;%29;%30; 

filename=strcat('RAW_',subject);

file=fopen(filename); 
data=fread(file, [4+sequenceSize qtyTrials]);
fclose(file);


%total right
win=0;

%noise=zeros(qtyTrials,sequenceSize);
subsampleMaleStim=[];
subsampleMaleAns=[];
subsampleMaleRight=[];
subsampleMaleWrong=[];
subsampleFemaleStim=[];
subsampleFemaleAns=[];
subsampleFemaleRight=[];
subsampleFemaleWrong=[];
subsampleConfident=[];
subsampleNonconfident=[];


%for i=1:qtyTrials
for i=51:qtyTrials
      
   mu0=data(2,i)
    noise(i,:)=(data(3:2+sequenceSize,i)-mu0)';
    
    
        if(data(2,i)==maleStart)
            subsampleMaleStim=[subsampleMaleStim; noise(i,:)]; 
            if(data(sequenceSize+3,i)==1)
                subsampleMaleRight=[subsampleMaleRight; noise(i,:)];
                win=win+1;
            else if(data(sequenceSize+3,i)==0)
                    subsampleMaleWrong=[subsampleMaleWrong; noise(i,:)];
                end
            end 
        else if(data(2,i)==femaleStart)
                subsampleFemaleStim=[subsampleFemaleStim; noise(i,:)];
                if(data(sequenceSize+3,i)==0)
                    subsampleFemaleRight=[subsampleFemaleRight; noise(i,:)];
                    win=win+1;
                else if(data(sequenceSize+3,i)==1)
                    subsampleFemaleWrong=[subsampleFemaleWrong; noise(i,:)];
                   end
                end
            end
        end
    
    if(data(sequenceSize+3,i)==1)
        subsampleMaleAns=[subsampleMaleAns; noise(i,:)]; 
    else if(data(sequenceSize+3,i)==0)
        subsampleFemaleAns=[subsampleFemaleAns; noise(i,:)];
        end
    end
        
    
    if(data(sequenceSize+4,i)==1)
        subsampleConfident=[subsampleConfident; noise(i,:)];
    else subsampleNonconfident=[subsampleNonconfident; noise(i,:)];
    end
    
    %if ((data(2,i)==maleStart && data(sequenceSize+3,i)==1) || (data(2,i)==femaleStart && data(sequenceSize+3,i)==0))
    %    win=win+1;
    %end
    success(i)=win/i;
    
    
   
end


frac=win/qtyTrials

xAxis=1:sequenceSize;

kernel=mean(subsampleMaleRight)+mean(subsampleMaleWrong)-mean(subsampleFemaleRight)-mean(subsampleFemaleWrong);

%size(subsampleMaleRight)
%size(subsampleMaleWrong)
%size(subsampleFemaleRight)
%size(subsampleFemaleWrong)

plot(xAxis,mean(noise),xAxis,kernel);
%plotMean;


%plot(xAxis,mean(subsampleMaleStim),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong));
%plot(xAxis,mean(subsampleMaleStim),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong)-mean(subsampleMaleStim));

%plot(xAxis,mean(noise),xAxis,mean(subsampleConfident)-mean(subsampleNonconfident));

%size(subsampleConfident)
%mean(subsampleConfident(:))
%size(subsampleNonconfident)
%mean(subsampleNonconfident(:))

%mean(subsampleMaleRight(:))
%mean(subsampleMaleWrong(:))
%mean(subsampleFemaleWrong(:)) 
%mean(subsampleFemaleRight(:))

trials=1:qtyTrials;
%plot(trials,success);
calibIndices=mean(data(3:2+sequenceSize,:))
calibAnswers=data(sequenceSize+3,:);
   
%hist(mean(noise').*data(sequenceSize+3,:));
 
%plot(calibIndices,calibAnswers,'x');

qtyWindows=20;
step=1;
for winIndex=1:qtyWindows
    
    trialsInWin(winIndex)=0;
    malesInWin(winIndex)=0;
    window=15+(winIndex)*step;
    windows(winIndex)=window;
    
    
    for trial=1:qtyTrials
        if(calibIndices(trial)>= window && calibIndices(trial) < window+step)
            trialsInWin(winIndex)=trialsInWin(winIndex)+1;
                if(calibAnswers(trial)==1)
                    malesInWin(winIndex)=malesInWin(winIndex)+1;
                end
        end
    end
    
    fracInWin(winIndex)=malesInWin(winIndex)/trialsInWin(winIndex);
    
end

trialsInWin
malesInWin
%plot(windows,fracInWin,'x');




end

